package com.dfbz.dao;

import com.dfbz.common.Constr;
import com.dfbz.dao.base.BaseDao;
import com.dfbz.model.User;
import com.dfbz.model.UserInfo;
import com.dfbz.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao<User> {


    public UserDao() {
        super(User.class);
    }

    /**
     * 根据用户名关联查询对应资料信息
     * @param username 用户名
     * @return Map对象，包含头像，部门名称，真实姓名
     */
    public Map<String,Object> getBaseInfoByName(String username) {
        Map<String,Object> map =null;
        String sql = "select u.id as user_id,u.username,ui.real_name,ui.pic,d.name as dept_name\n" +
                "from user u\n" +
                "left join user_info ui on u.id=ui.user_id\n" +
                "left join dept d on u.dept_id = d.id\n" +
                "where username = ?";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql, new MapHandler(), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;

    }

    public Map<String, Object> getUserInfoById(String userId) {
        Map<String,Object> map =null;
        String sql = "select d.name as dept_name,u.*,ui.*,(select count(*) from user_focus where user_focus_id = ?) as fan\n" +
                "from user u \n" +
                "left join user_info ui on ui.user_id = u.id\n" +
                "left join dept d on d.id = u.dept_id\n" +
                "where u.id=?\n";
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        try {
            map = qr.query(sql, new MapHandler(), userId,userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }

    public List<Map<String,Object>> getUserList(String userId,int currentPage, String searchName) {
        String sql = "select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,\n" +
                "(select id from user_focus where user_id = ? and user_focus_id=ui.user_id) as ufid\n" +
                "from user_info ui\n" +
                "limit ?,?";
        if (null!=searchName){
            sql="select ui.user_id,ui.real_name,ui.age,ui.gender,ui.introduction,\n" +
                    "(select id from user_focus where user_id = ? and user_focus_id=ui.user_id) as ufid\n" +
                    "from user_info ui\n" +
                    "where ui.real_name like ?\n" +
                    "limit ?,?\n";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = null;
        try {
            if (null!=searchName){
                list = qr.query(sql, new MapListHandler(),userId,"%"+searchName+"%", (currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }else {
                list = qr.query(sql, new MapListHandler(), userId,(currentPage - 1) * Constr.PAGE_SIZE,Constr.PAGE_COUNT);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getUserTotal(String searchName){
        long total=0L;
        String sql = "select count(*) as totalUser from user_info;";
        if (null!=searchName){
            sql = "select count(*) as totalUser from user_info where real_name like ?";
        }
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            if (null!=searchName){
                map = qr.query(sql, new MapHandler(),"%"+searchName+"%");
            }else {
                map = qr.query(sql, new MapHandler());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (null!=map){
            total = Long.parseLong(map.get("totalUser").toString());
        }
        return total;
    }

    public List<Map<String, Object>> findUsers(String userId, int currentPage, String name) {
        return null;
    }
}
